<?php
	require_once('LoginController.php');

	class LoginHandler {
		
		public $m_login = 'loggedIn';
		
		/**
		 * Check if user is logged in
		 * @return boolean
		 */
		public function IsLoggedIn() {
			if (isset($_SESSION[$this->m_login])) {
				return true;
			}
			return false;
		}

		/**
		 * @param string $user Username to try
		 * @param string $password Password to try
		 * @return boolean succesfull login
		 */
		public function DoLogin($user, $password) {
			switch($user) {
				case "ratt":
						if ($password == "ratt") {
							$_SESSION[$this->m_login] = $user;
							return true;
						}
					break;
				case "anvandare":
						if ($password == "losen") {
							$_SESSION[$this->m_login] = $user;
							return true;
						}
					break;
			}
			
			return false;
		}
		
		/**
		 * Sets login status to false
		 */
		public function DoLogout() {
			if(isset($_SESSION[$this->m_login])) {
				$_SESSION[$this->m_login] = false;
			}
		}
		
		/**
		 * Echo test data
		 * @return boolean
		 */
		public function Test() {
				
			$this->DoLogOut();
			
			if($this->IsLoggedIn()) {
				echo "IsLoggedIn eller DoLogout fungerar inte som de ska";
				return false;
			}
			
			if($this->DoLogin('fel', 'fel')) {
				echo "DoLogin fungerar inte";
				return false;
			}
			
			if(!$this->DoLogin('ratt', 'ratt')) {
				echo "DoLogin fungerar inte";
				return false;
			}
			
			if(!$this->IsLoggedIn()) {
				echo "IsLoggedIn fungerar inte";
				return false;
			}
			
			$this->DoLogout();
			
			if($this->DoLogin('ratt', 'fel')) {
				echo "DoLogin med rätt användarnamn och fel lösenord fungerar inte";
				return false;
			}
			
			return true;
		}
	}
?>